package com.example.night.service.MysqlService.PromoteGoods;

import com.example.night.data.Good.GoodPackage;
import com.example.night.data.Good.GoodsRanking;
import com.example.night.data.promoteGood.ReputationRanking;
import com.example.night.data.promoteGood.ReputationRankingRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ReputationRankingMysqlService {

    @Autowired
    private ReputationRankingRepository rankingRepository;

    // ✅ 修改 save 方法，接收 List<GoodsRanking> 并转换
    public void save(List<GoodsRanking> goodsRankingList, String date) {
        for (GoodsRanking goodsRanking : goodsRankingList) {
            ReputationRanking ranking = new ReputationRanking();

            // 复制商品信息
            GoodPackage goodPackage = goodsRanking.getGoodPackage();  // 获取商品对象
            ranking.setGoodPackage(goodPackage);  // 设置商品信息

            // 复制排名信息
            ranking.setRanking(goodsRanking.getRanking());

            // 设置日期
            ranking.setDate(date);

            // 保存到数据库
            rankingRepository.save(ranking);
        }
    }

    // ✅ 修改 findByDate 方法，返回 List<GoodsRanking>
    public List<GoodsRanking> findByDate(String date) {
        // 查询数据库中的 ReputationRanking
        List<ReputationRanking> rankings = rankingRepository.findByDate(date);

        // 将查询结果转换为 GoodsRanking 列表
        return rankings.stream()
                .map(ranking -> new GoodsRanking(ranking.getGoodPackage(), ranking.getRanking()))
                .collect(Collectors.toList());
    }
}
